package com.apes.scm.account.account.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.account.account.model.BatchOfFrozen;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.scm.account.account.repository
 * @ClassName: BatchOfFrozenRepository
 * @Author: pyb
 * @Description: 批次冻结数明细账数据层
 * @CreateTime: 2020-12-01 16:02
 * @Version: 1.0
 */
public interface BatchOfFrozenRepository extends CustomRepository<BatchOfFrozen, Long> {
    /**
     * 根据来源单号 类型 批次号找最原始的冻结明细行
     *
     * @param sourceBusinessType 来源单据类型
     * @param orderNumber 来源单号
     * @param inventoryBatchId 批次号
     * @return BatchOfFrozen
     */
    @Query("select t from BatchOfFrozen t where t.sourceType=?1 and t.source=?2 and t.batchId=?3 and t.changeQty>0 and (t.changeQty-t.thawingQty)>0")
    List<BatchOfFrozen> findBySource(String sourceBusinessType, String orderNumber, Long inventoryBatchId);

    @Query("SELECT T FROM BatchOfFrozen T WHERE T.sourceBusinessType = ?1 AND orderNumber = ?2 and T.product.id = ?3 ")
    List<BatchOfFrozen> findByCurrentNumber(String sourceBusinessType, String orderNumber, String productId);

}
